/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package twitterSearch;

import java.io.IOException;
import java.util.LinkedList;
import lucene.Analyzers.AnalyzerUtils;
import old.StemmingPositionStopTwitterAnalyzer_NoHashtags;
import lucene.Analyzers.StemmingPositionStopTwitterAnalyzer;
import lucene.Analyzers.SynonymPositionStopTwitterAnalyzer;
import lucene.Analyzers.stantard.StandardAnalyzer;
import old.MetaphoneReplacementAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import twitter4j.Tweet;

/**
 *
 * @author Livia
 */
public class TwitterLuceneIndex{
    
    Directory dir;    
    private final Analyzer[] analyzers;
    
    public TwitterLuceneIndex(Directory dir) throws IOException{
        this.dir = dir;
        analyzers = new Analyzer[] {
            //new StemmingPositionStopTwitterAnalyzer_NoHashtags(), 
            new StemmingPositionStopTwitterAnalyzer(),
            //new SynonymPositionStopTwitterAnalyzer(2),
            //new MetaphoneReplacementAnalyzer(),
            //new StandardAnalyzer()
            
        };
    }
    
    public void addTweets(java.util.List<Tweet> tweets) throws IOException {

        IndexWriter writer = new IndexWriter(dir, getAnalyzer(), true);
        writer.setUseCompoundFile(isCompound());
        
        for (int i = 0; i < tweets.size(); i++) {
            //umm doc diferente por tweet
            Document doc = new Document();
            doc.add(Field.Keyword("user", tweets.get(i).getFromUser())); 
            //com vetor
            doc.add(Field.Text("content", tweets.get(i).getText(),true));
            writer.addDocument(doc);
        }
        writer.optimize();
        writer.close();
    }
    
    public void addTweetsNoBlanks(java.util.List<Tweet> tweets) throws IOException {

        IndexWriter writer = new IndexWriter(dir, getAnalyzer(), true);
        writer.setUseCompoundFile(isCompound());
        
        for (int i = 0; i < tweets.size(); i++) {
            //umm doc diferente por tweet, so se tiver mais de 0 tokens!!!!!!!!!!!
            if (AnalyzerUtils.tokensFromAnalysis(getAnalyzer(), tweets.get(i).getText()).length >= 1){                
                Document doc = new Document();
                doc.add(Field.Keyword("user", tweets.get(i).getFromUser())); 
                //com vetor
                doc.add(Field.Text("content", tweets.get(i).getText(),true));
                writer.addDocument(doc);
            }
        }
        writer.optimize();
        writer.close();
    }

    protected Analyzer getAnalyzer() {
        return new StemmingPositionStopTwitterAnalyzer();
    }

    private boolean isCompound() {
        return true;
    }
    
    public void analyzeTokens(java.util.List<Tweet> tweets) throws IOException {
                
        for (int j = 0, i = 0; j<tweets.size(); j++){
            String text = tweets.get(j).getText();
            System.out.println("---- TWEET #" + j + "---");
            
            //so mostro analise de tweets que vao para o INDEX no final
            if (AnalyzerUtils.tokensFromAnalysis(getAnalyzer(), tweets.get(j).getText()).length >= 1){
                System.out.println("---- DOC NO INDEX #" + i + "---");
                i++;
            
                System.out.println("Analyzing \"" + text + "\"");  

                for (int k = 0; k < analyzers.length; k++) {
                    Analyzer analyzer = analyzers[k];
                    String name = analyzer.getClass().getName();
                    name = name.substring(name.lastIndexOf(".") + 1);
                    System.out.println(" " + name + ":");
                    System.out.print(" ");
                    AnalyzerUtils.displayTokens(analyzer, text);
                    System.out.println("\n");
                }
            }
        }
     }
     
    public LinkedList<String> analyzeString(String s) throws IOException {
        LinkedList<String> list = new LinkedList <String>();     
        
        org.apache.lucene.analysis.Token[] tokens = 
                AnalyzerUtils.tokensFromAnalysis(this.getAnalyzer(), s);
            
        for (int i = 0; i < tokens.length; i++)
            list.add(tokens[i].termText());

        return list;
        }
     
     public static void main (String args[]) throws IOException{
         String indexDir = "C:\\index-dir";
         FSDirectory dir = FSDirectory.getDirectory(indexDir, true);
         TwitterLuceneIndex index = new TwitterLuceneIndex(dir);
         for (int k = 0; k < index.analyzers.length; k++) {
                    Analyzer analyzer = index.analyzers[k];
                    String name = analyzer.getClass().getName();
                    name = name.substring(name.lastIndexOf(".") + 1);
                    System.out.println(" " + name + ":");
                    System.out.print(" ");
                    AnalyzerUtils.displayTokens(analyzer, 
 "@CoJoLove man u kno u gotta be inside b4 them street lights come on hahhahaha");
                    System.out.print("\n");
                    AnalyzerUtils.displayTokens(analyzer, 
 "http://t.co/nX7ZJwf4 Street artists help graffiti fight");
                    System.out.print("\n");
                     AnalyzerUtils.displayTokens(analyzer, 
 "@nathaliaiole the abandon orchard on the street by kyle and luis's");
                    System.out.print("\n");
                    AnalyzerUtils.displayTokens(analyzer, 
 "I like my women both book and street smart");
                    
                    System.out.println("\n");
                }
     }
          
}